// pages/store/index.js
const storageCURD = require("../../utils/StorageCURD")
const requests = require("../../utils/request")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    interval:1000,//自动切换时间间隔
    duration:500,//滑动动画时长
    activeKey: 0,
    banner1:'',
    banner2:'',
    searchvalue:'',
    products1:[],
    icon1:'',
    icon2:'',
    icon3:'',
    icon4:'',

    product1Swiper1:'',
    product1Swiper2:'',
    product1Swiper3:'',
    product1Swiper4:'',
    product1Swiper5:''

  },
  onChangeSideBar(event)
  {
    this.setData({
      activeKey:event.detail
    })
  },
  gotoDetail(e)
  {
    // console.log(e.currentTarget.dataset.id)
    const goodsId = e.currentTarget.dataset.id
    const image = e.currentTarget.dataset.image
    const detail = e.currentTarget.dataset.detail
    const price = e.currentTarget.dataset.price
    const title=  e.currentTarget.dataset.title
    const des = e.currentTarget.dataset.des
    wx.navigateTo({
      url: `./productDetail/index?goodsId=${goodsId}&image=${image}&detail=${detail}&price=${price}&title=${title}&des=${des}`,
    })
  },
  gotoShoppingcart()
  {
    wx.navigateTo({
      url: './shoppingcart/index',
    })
  },
  async addCard(e)
  {
    const product = e.currentTarget.dataset.product
    console.log(product)
    const {goodsId,title,thumb,price} = product
    // console.log(price.substring(0,price.length-2))
    await storageCURD.add('shoppingCart',{id:goodsId,title:title,image:thumb,num:1,price,selected:true
    })
    wx.showToast({
      title: '操作成功！', // 标题
      icon: 'success',  // 图标类型，默认success
      duration: 1500  // 提示窗停留时间，默认1500ms
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {

    let res1 = await requests.downloadFile('goods/banner1.jpg')
    let res2 = await requests.downloadFile('goods/banner2.jpg')
    this.setData({
      banner1:res1,
      banner2:res2,
    })
    Promise.all([requests.downloadFile('static/商城icon/兑换码.png'),requests.downloadFile('static/商城icon/秒杀.png'),requests.downloadFile('static/商城icon/兑换码.png'),requests.downloadFile('static/商城icon/砍价.png')]).then((res)=>{
      this.setData({
      icon1:res[0],
      icon2:res[1],
      icon3:res[2],
      icon4:res[3],
    })
    })
    // let res3 = await requests.downloadFile('static/商城icon/兑换码.png')
    // let res4 = await requests.downloadFile('static/商城icon/秒杀.png')
    // let res5 = await requests.downloadFile('static/商城icon/兑换码.png')
    // let res6 = await requests.downloadFile('static/商城icon/砍价.png')

    // this.setData({
    //   icon1:res3,
    //   icon2:res4,
    //   icon3:res5,
    //   icon4:res6,
    // })

  

    /**
     * 获取商品信息
     */
   let res = await requests.goodsInterface(null,null,null,null,null,null,null,'getAll')
  //  console.log('全部商品',res.data.data.value)
   let goods = res.data.data.value
   for(let i=0;i<goods.length;i++)
   {
    let res = await requests.downloadFile(goods[i].thumb)
    goods[i].thumb=res
    if(i == goods.length-1)
    {
      this.setData({
        products1:goods
      })
    }
   }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})